import os
import sys
import django
import datetime

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(BASE_DIR)
sys.path.insert(0, BASE_DIR)
os.environ['DJANGO_SETTINGS_MODULE'] = 'finance_c2c.settings'
django.setup()

from apps.czb.client import get_czb_api
from apps.czb.models import GasStationInfo, OilInfo
from core.wchat.wx_base import WxBase

wx_base = WxBase()

upGasName = {"DQ000002143": "大桥石化（荥科站）",
             "DX330340840": "大桥石化（大学路站）",
             "CJ245371624": "大桥石化（长江路站）",
             "XH250798996": "大桥石化（卧龙站）",
             "RH254672443": "大桥石化（汝河路站）",
             "DQ000001468": "大桥石化（北四环站）",
             "JB526335645": "大桥石化（金杯站）",
             "BH637276543": "大桥石化（北环路站）",
             "XL245727435": "大桥石化（兴隆站）",
             "ZA146512435": "大桥石化（中澳站）",
             "PT257642777": "大桥石化（圃田站）",
             "XS245726243": "大桥石化（西四环站）",
             "DQ000001811": "大桥石化（江山路站）",
             "DQ000001807": "大桥石化（南三环站）",
             "DQ000001808": "大桥石化（柴郭站）",
             "HH654327212": "大桥石化（淮河路站）"
             }


# 创建车主邦油站油枪信息
def crete_gas_oil_czb(result):
    for gas in result:
        oilPriceList = gas.pop("oilPriceList")
        gasId = gas.pop("gasId")
        try:
            gas_instance, create = GasStationInfo.objects.get_or_create(gasId=gasId)
            if "团油" in gas.get("gasName"):
                if upGasName.get(gasId):
                    gas_instance.gasName = upGasName.get(gasId)
                else:
                    gas_instance.gasName = gas.get("gasName")
                    user_id = [
                        "ojh4pt-NhPINlEqorner59MUt5kc",  # 刘，
                        "ojh4ptynBWEzmimzXxZFGtiOJAsY",  # 李，
                        "ojh4pt7nqVr7HbAEQlb1-jAf_WlY",  # 静，
                        "ojh4pt2CWsszLCsollqexwxiOAI0",  # 田，
                    ]
                    content = "加油站id:%s\n加油站名称：%s\n地址：%s,%s,%s" % (
                        gasId, gas.get("gasName"), gas.get("cityName"), gas.get("countyName"), gas.get("gasAddress"))
                    for i in user_id:
                        wx_base.client.message.send_text(i, content)
            else:
                gas_instance.gasName = gas.get("gasName")
            gas_instance.gasType = gas.get("gasType")
            gas_instance.gasLogoSmall = gas.get("gasLogoSmall")
            gas_instance.gasLogoBig = gas.get("gasLogoBig")
            gas_instance.gasAddress = gas.get("gasAddress")
            gas_instance.gasAddressLongitude = gas.get("gasAddressLongitude")
            gas_instance.gasAddressLatitude = gas.get("gasAddressLatitude")
            gas_instance.provinceCode = gas.get("provinceCode")
            gas_instance.cityCode = gas.get("cityCode")
            gas_instance.countyCode = gas.get("countyCode")
            gas_instance.provinceName = gas.get("provinceName")
            gas_instance.cityName = gas.get("cityName")
            gas_instance.countyName = gas.get("countyName")
            gas_instance.isInvoice = gas.get("isInvoice")
            gas_instance.update_time = datetime.datetime.now()
            gas_instance.save()
            if create:
                oil_list = []
                for i in oilPriceList:
                    i["gs_station"] = gas_instance
                    oil_list.append(OilInfo(**i))
                OilInfo.objects.bulk_create(oil_list)
        except Exception as e:
            print(gas.get("gasName"))
            print(e)
            break


a = [{'gasId': 'my000001126', 'gasName': 'my gas', 'oilPriceList': [
    {'oilNo': -40, 'oilName': '-40#', 'priceYfq': '5.55', 'priceGun': '5.55', 'priceOfficial': '5.66', 'oilType': 2,
     'gunNos': [{'gunNo': 1}, {'gunNo': 2}, {'gunNo': 6}, {'gunNo': 7}, {'gunNo': 8}, {'gunNo': 9}, {'gunNo': 10},
                {'gunNo': 11}, {'gunNo': 12}]},
    {'oilNo': -20, 'oilName': '-20#', 'priceYfq': '23.00', 'priceGun': '23.00', 'priceOfficial': '23.00', 'oilType': 2,
     'gunNos': None},
    {'oilNo': 15, 'oilName': 'CNG', 'priceYfq': '2.00', 'priceGun': '2.00', 'priceOfficial': '2.00', 'oilType': 3,
     'gunNos': [{'gunNo': 90}, {'gunNo': 91}]},
    {'oilNo': 16, 'oilName': 'LNG', 'priceYfq': '8.65', 'priceGun': '8.65', 'priceOfficial': '8.93', 'oilType': 3,
     'gunNos': [{'gunNo': 0}, {'gunNo': 93}]},
    {'oilNo': 90, 'oilName': '90#', 'priceYfq': '6.21', 'priceGun': '6.21', 'priceOfficial': '6.45', 'oilType': 1,
     'gunNos': [{'gunNo': 4}, {'gunNo': 5}]},
    {'oilNo': 92, 'oilName': '92#', 'priceYfq': '8.35', 'priceGun': '8.35', 'priceOfficial': '8.58', 'oilType': 1,
     'gunNos': [{'gunNo': 3}, {'gunNo': 20}, {'gunNo': 21}, {'gunNo': 22}, {'gunNo': 23}, {'gunNo': 24}, {'gunNo': 25},
                {'gunNo': 26}, {'gunNo': 27}, {'gunNo': 28}, {'gunNo': 29}, {'gunNo': 30}, {'gunNo': 31}, {'gunNo': 32},
                {'gunNo': 33}, {'gunNo': 34}, {'gunNo': 35}, {'gunNo': 36}, {'gunNo': 37}, {'gunNo': 38}, {'gunNo': 39},
                {'gunNo': 40}]},
    {'oilNo': 98, 'oilName': '98#', 'priceYfq': '6.11', 'priceGun': '6.11', 'priceOfficial': '6.33', 'oilType': 1,
     'gunNos': [{'gunNo': 98}]},
    {'oilNo': 99, 'oilName': '101#', 'priceYfq': '5.00', 'priceGun': '5.00', 'priceOfficial': '6.00', 'oilType': 1,
     'gunNos': [{'gunNo': 99}]}]}, {'gasId': 'YC000011414', 'gasName': '预存款油站', 'oilPriceList': [
    {'oilNo': -40, 'oilName': '-40#', 'priceYfq': '4.02', 'priceGun': '5.00', 'priceOfficial': '6.65', 'oilType': 2,
     'gunNos': None},
    {'oilNo': 16, 'oilName': 'LNG', 'priceYfq': '7.68', 'priceGun': '7.65', 'priceOfficial': '7.91', 'oilType': 3,
     'gunNos': [{'gunNo': 22}]},
    {'oilNo': 90, 'oilName': '90#', 'priceYfq': '5.59', 'priceGun': '6.57', 'priceOfficial': '6.92', 'oilType': 1,
     'gunNos': [{'gunNo': 1}]},
    {'oilNo': 92, 'oilName': '92#', 'priceYfq': '5.29', 'priceGun': '6.27', 'priceOfficial': '6.88', 'oilType': 1,
     'gunNos': [{'gunNo': 2}]},
    {'oilNo': 95, 'oilName': '95#', 'priceYfq': '5.85', 'priceGun': '6.83', 'priceOfficial': '7.12', 'oilType': 1,
     'gunNos': None}]}, {'gasId': 'QJ000011408', 'gasName': '枪价基础加油站', 'oilPriceList': [
    {'oilNo': 90, 'oilName': '90#', 'priceYfq': '5.22', 'priceGun': '6.20', 'priceOfficial': '6.90', 'oilType': 1,
     'gunNos': [{'gunNo': 2}]},
    {'oilNo': 92, 'oilName': '92#', 'priceYfq': '9.44', 'priceGun': '11.10', 'priceOfficial': '12.10', 'oilType': 1,
     'gunNos': [{'gunNo': 1}]},
    {'oilNo': 98, 'oilName': '98#', 'priceYfq': '6.81', 'priceGun': '7.79', 'priceOfficial': '8.20', 'oilType': 1,
     'gunNos': None}]}]


# for i in a:
#     gasId = i.get("gasId")
#     gas_instance = GasStationInfo.objects.get(gasId=gasId)
#     oilPriceList = i.get("oilPriceList")
#     for oil in oilPriceList:
#         print(oil)
#         OilInfo.objects.update_or_create(
#             defaults={"oilNo": oil.get("oilNo"), "gs_station": gas_instance},
#             others={
#                 "oilName": oil.get("oilName"),
#                 "priceYfq": oil.get("priceYfq"),
#                 "priceGun": oil.get("priceGun"),
#                 "priceOfficial": oil.get("priceOfficial"),
#                 "oilType": oil.get("oilType"),
#             })

def update_gas():
    czb_api = get_czb_api("youpinhuiyuan1.0")
    response = czb_api.query_gas_info_list()
    result = response.get("result")
    print(len(result))
    crete_gas_oil_czb(result)


if __name__ == '__main__':
    # pass
    update_gas()
    # czb_api = get_czb_api("youpinhuiyuan")
    # response = czb_api.query_price_by_phone("DM425228581", "18612386233")
    # print(response)
    # crete_go(response.get("result"))
    #
    # result = czb_api.platform_login("186****6233")
    # print(result)
    # r = {'app_key': 'kalaibaoyufu1.0', 'orderSn': 'CZBH148081235023118',
    #      'outerOrderId': '19102117512053931553', 'refundAmount': '25258',
    #      'refundTradeNo': 'QJ0000114081910211XcJU7',
    #      'sign': '6874739e090bcb04eaf565a11d6eba15', 'timestamp': '1571651584783'}
    # sign = r.pop("sign")
    # v_sign = czb_api.get_sign(r, czb_api.app_secret)
    # print(v_sign, sign)
    # print(v_sign == sign)

    # result = czb_api.query_depreciate_amount("DM425228581", "95", "100.00", "18612386233")
    # print(result)
